//
//  Extension+UIBarButtonItem.swift
//  MyWeibo
//
//  Created by 黄勇锋 on 16/7/24.
//  Copyright © 2016年 黄勇锋. All rights reserved.
//

import UIKit

//类扩展
extension UIBarButtonItem{
    
    convenience init(title:String? = nil ,imgName:String? = nil,target:AnyObject?,action:Selector) {
        
        self.init()
        
        let button = UIButton()
        
        //给 button 添加属性之前,需要先判断 传进来的参数是否有值
        if let tit = title {
            
            button.setTitle(tit, forState: UIControlState.Normal)
            button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
            button.titleLabel?.font = UIFont.systemFontOfSize(15)
            
            //设置禁用掉的时候显示的颜色
            button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Disabled)
        }
        
        if let img = imgName {
            
            button.setImage(UIImage(named: img), forState: UIControlState.Normal)
        }
        
        //添加点击事件
        button.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        
       //自适应尺寸
       button.sizeToFit()
        
       self.customView = button
    }
    
}



